package com.zhangjun;

import com.zhang.view.chat.ChatController;
import com.zhang.view.chat.IChatMethod;
import com.zhang.view.login.ILoginMethod;
import com.zhang.view.login.LoginController;
import com.zhangjun.application.UIService;
import com.zhangjun.event.ChatEvent;
import com.zhangjun.event.LoginEvent;
import com.zhangjun.infrastructure.util.CacheUtil;
import com.zhangjun.socket.NettyClient;
import io.netty.channel.Channel;
import javafx.stage.Stage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.*;

/**
 * 博  客：http://bugstack.cn
 * 公众号：bugstack虫洞栈 | 沉淀、分享、成长，让自己和他人都能有所收获！
 * create by 小傅哥 on @2020
 */
public class Application extends javafx.application.Application {

    private Logger logger = LoggerFactory.getLogger(Application.class);



    //默认线程池

    private static ExecutorService executorService = Executors.newFixedThreadPool(2);

    private ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);



    @Override

    public void start(Stage primaryStage) throws Exception {

        // 1. 启动窗口

        IChatMethod chat = new ChatController(new ChatEvent());

        ILoginMethod login = new LoginController(new LoginEvent(), chat);

        login.doShow();



        UIService uiService = new UIService();

        uiService.setChat(chat);

        uiService.setLogin(login);



        // 2. 启动socket连接

        logger.info("NettyClient连接服务开始 inetHost：{} inetPort：{}", "127.0.0.1", 7397);

        NettyClient nettyClient = new NettyClient(uiService);

        Future<Channel> future = executorService.submit(nettyClient);

        Channel channel = future.get();

        if (null == channel) throw new RuntimeException("netty client start error channel is null");



        while (!nettyClient.isActive()) {

            logger.info("NettyClient启动服务 ...");

            Thread.sleep(500);

        }

        logger.info("NettyClient连接服务完成 {}", channel.localAddress());



    }



    public static void main(String[] args) {

        launch(args);

    }
}
